﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using plust.Clases;
using plust.Persistencia;
using System.Data;
using Microsoft.Win32;
using FirstFloor.ModernUI.Windows.Controls;
using System.Diagnostics;
using Microsoft.Office.Interop.Excel;

namespace plust.Content
{
    /// <summary>
    /// Interaction logic for ReportesProductos.xaml
    /// </summary>
    public partial class ReportesProductos : UserControl
    {
        private Microsoft.Office.Interop.Excel.Application excel;

        private PersistenciaDB persistencia = new PersistenciaDB();

        public ReportesProductos()
        {
            InitializeComponent();

            this.pb.IsIndeterminate = false;

            this.pb.Visibility = System.Windows.Visibility.Hidden;

            this.cbReporte.Items.Add("Todos los productos");

            this.cbReporte.SelectedIndex = 0;

        }

        private async void generarReporte()
        {
            this.spForm.Visibility = System.Windows.Visibility.Hidden;

            this.pb.IsIndeterminate = true;
            this.pb.Visibility = System.Windows.Visibility.Visible; 

            var slowTask = Task<Microsoft.Office.Interop.Excel.Application>.Factory.StartNew(() => generarReporteTask());

            await slowTask;

            excel = slowTask.Result;

            this.pb.Visibility = System.Windows.Visibility.Hidden;
            this.pb.IsIndeterminate = false;
            this.spForm.Visibility = System.Windows.Visibility.Visible;

            ModernDialog.ShowMessage("Reporte Generado Correctamente.", "CORRECTO", MessageBoxButton.OK);

            this.excel.Visible = true;

            Keyboard.Focus(this.cbReporte);

        }

        private Microsoft.Office.Interop.Excel.Application generarReporteTask()
        {
            System.Data.DataTable dt = null;

            dt = persistencia.obtenerProductosReporte();

            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Add(true);
            
            int iCol = 0;
            foreach (DataColumn c in dt.Columns)
            {
                iCol++;
                excel.Cells[1, iCol] = c.ColumnName;
                excel.Cells[1, iCol].Interior.Color = System.Drawing.Color.PaleGreen;
            }

            int iRow = 0;
            foreach (DataRow r in dt.Rows)
            {
                iRow++;
                iCol = 0;
                foreach (DataColumn c in dt.Columns)
                {
                    iCol++;
                    excel.Cells[iRow + 1, iCol] = r[c.ColumnName];

                    if (c.ColumnName.Equals("sku"))
                    {
                        excel.Cells[iRow + 1, iCol].NumberFormat = "0";
                    }

                    if (c.ColumnName.Equals("stock_real") && int.Parse(r["stock_real"].ToString()) - int.Parse(r["stock_critico"].ToString()) < 0)
                    {
                        excel.Cells[iRow + 1, iCol].Interior.Color = System.Drawing.Color.OrangeRed;
                        excel.Cells[iRow + 1, iCol].NoteText("Stock bajo el critico");
                    }
                    else 
                    {
                        excel.Cells[iRow + 1, iCol].Interior.Color = System.Drawing.Color.AliceBlue;
                    }                      
                }
            }

            Microsoft.Office.Interop.Excel.Range rangeStyles = excel.get_Range("A1", "I99999");
            rangeStyles.Columns.AutoFit();

            
            return excel;
        }

        private void btGenerarReporte_Click(object sender, RoutedEventArgs e)
        {
            this.generarReporte();
        }
    }
}
